package com.example.common.config;

import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = {"com.example.app.mapper", "com.example.yin.dao"},
        sqlSessionFactoryRef = "sqlSessionFactoryApp"
//       ， sqlSessionTemplateRef = "sqlSessionTemplateApp"
)
public class DataSourceAppConfig {

    @Autowired
    MybatisPlusInterceptor mybatisPlusInterceptor;
    @Autowired
    MyMetaObjectHandler myMetaObjectHandler;

    @Bean(name = "sqlSessionFactoryApp")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("app") DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        bean.setDataSource(dataSource);

        Resource[] resources1 = new PathMatchingResourcePatternResolver().getResources("classpath*:com/example/app/mapper/xml/*.xml");
        Resource[] resources2 = new PathMatchingResourcePatternResolver().getResources("classpath*:/mapper/*.xml");

        ArrayUtils.addAll(resources1, resources2);

        bean.setMapperLocations(ArrayUtils.addAll(resources1, resources2));
        bean.setGlobalConfig(globalConfig());
        bean.setPlugins(mybatisPlusInterceptor);
        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.setMapUnderscoreToCamelCase(true);
        // 配置打印sql语句
        configuration.setLogImpl(StdOutImpl.class);
        bean.setConfiguration(configuration);
        return bean.getObject();
    }

    @Bean(name = "transactionManagerApp")
    public DataSourceTransactionManager transactionManager(@Qualifier("app") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

//    @Bean(name = "sqlSessionTemplateApp")
//    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactoryApp") SqlSessionFactory sqlSessionFactory) {
//        return new SqlSessionTemplate(sqlSessionFactory);
//    }

    /**
     * 自动插入创建和更新时间
     * 多数据源下要创建多个，或者不创建，
     * 不然saveBatch等批量操作方法会报错，
     * 因为save方法是获取baseMapper，
     * 而批量操作的saveBatch方法是从全局配置 GlobalConfig 里获取的。
     */
    public GlobalConfig globalConfig() {
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setMetaObjectHandler(myMetaObjectHandler);
        return globalConfig;
    }
}
